<?
require_once 'clogin.inc.php';
require_once 'admin_head.php';
?>

<? 
$uid = $_SESSION["uid"];
$model_name ='分类';
if ($_POST['action'] == 'edit'){//编辑动作
	$id = intval($_POST['id']);
	$pid = $_POST['pid'];
	$component = $_POST['component'];
	$component2 = $_POST['component2'];
	$name = $_POST['name'];
	$displayid = $_POST['displayid'];
	empty($displayid) ? $displayid = 0 : 0;
	$uid = $_SESSION["uid"];
	$path ='';
	
	
	if (empty($id)) {
		$sql = "INSERT INTO {$tablepre}channel (pid, component, name, path, displayid,uid) 
				VALUES('$pid', '$component', '$name','$path', $displayid,'$uid')";
		$edit = '添加';		
	}else{
		if($component2 !=$component){
			$sql = "SELECT * FROM {$tablepre}{$component2} WHERE cid = $id ";
			$result=mysql_query($sql); 
			if(@mysql_num_rows($result) > 0){		
				 showmsg("该分类下还有内容,不能进行更改！",'成功',"return",'返回');
				exit();
			};		
		}
			
		$sql = "UPDATE {$tablepre}channel SET pid = '$pid', component = '$component', name = '$name', path = '$path', displayid = $displayid WHERE id = $id LIMIT 1";
	
		$edit = '修改';	
	} 
	mysql_query($sql); 	
	if (empty($id)) {
		$id=mysql_insert_id();
	}
	

 	update_path($id);
	if ( mysql_affected_rows() == 1 ) {
		 showmsg("{$model_name}{$edit}成功！",'成功',"return",'返回');
	}else{
		 showmsg("{$model_name}{$edit}失败或没有修改！",'失败',"?id=$id",'返回');
	}
	
}
if ($_GET['id']) {
	$id = intval($_GET['id']);
	if ($_GET['action'] == 'del'){//删除
		$edit = '删除';	
		$sql = "DELETE FROM {$tablepre}channel WHERE id = $id LIMIT 1";
		mysql_query($sql);
	
		showmsg("{$edit}成功！",'成功',"../frmView_right.php",'返回');
		exit();
		
	}
	$sql = "SELECT * FROM {$tablepre}channel WHERE id = $id ";
	$result=mysql_query($sql); 
	$tchannels = mysql_fetch_array($result);
}else{
	$pid = $_GET['pid'];
	$component = $_GET['component'];
	
}
/*$sql = "SELECT * FROM  {$tablepre}channel"; //分类
$result=mysql_query($sql); 
while($query = mysql_fetch_array($result)) {
$channels[]=$query;
}
*/
$cat_select = cat_list($tchannels['id'],$tchannels['pid'],'pid');

?>

<p>&nbsp;</p>

<div class="edit">
<div class="title"><?php 
if ($_GET['id']) {
	echo "编辑： $tchannels[name]  信息 [<a href=\"admin_channel.php\">添加一个新的</a>]";
}else{
	echo "添加分类目录";
}
?></div>
<form id="form1" name="form1" method="post" action="">

<input name="action" type="hidden" value="edit" />
<input name="id" type="hidden" value="<? echo $tchannels['id'] ?>" />
<input name="component2" type="hidden" value="<? echo $tchannels['component'] ?>" />
	<br />
	<p>
	  <label>上级分类：</label>
	   <?php echo $cat_select ?>
		</p>
	<p>
	  <label>分类类型：</label>
	  <?php 
	  $components = array('note'=>'文章','site'=>'站点','blog'=>'日记','adds'=>'通讯录')
	    ?>
      <select name="component">
        <option value='0'>--请选择类型--</option>
        <?php foreach($components as $key1 =>$c1){
			
		?>
        <option value="<?php echo $key1 ?>" <?php if ( $component == $key1 |$tchannels['component'] == $key1) { echo 'selected';}?>><? echo $c1 ?></option>
        <?php 
		  }?>
      </select>
</p>
	<p></p>
	<p>
	  <label>分类名称：</label>
        
          <input name="name" type="text" id="name" value="<? echo $tchannels['name'] ?>" size="40" />
  </p>
  <p> <label>
     
        显示位置：</label> 
        
          <input name="displayid" type="text" id="displayid" value="<? echo $tchannels['displayid'] ?>" size="5" />
   </p>
    <p>
         
          <input type="submit" name="Submit2" value="提交" />
        
    </p>
</form></div>
<p>&nbsp;</p>
</body>
</html>